package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.ProdInstDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.scene.AbstractInstService;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.service.util.CustInfoEncryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Iterator;
import java.util.List;

/**
 * 内存对象 manager 对外能力提供的通用方法的 service 封装
 *
 * @author G
 * @since 2019-6-10
 */
@Service("querySceneCommonService")
public class QuerySceneCommonService extends AbstractInstService implements IQuerySceneCommonService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    /**
     * 获取接入品实例所属的所有上级销售品实例(成员明细关系)
     *
     * @param custId 客户id
     * @param prodInstId 接入品实例id
     * @return 礼包&套餐&可选包&促销包销售品实例列表
     */
    @Validated
    DataResult<List<OfferInstDTO>> listAllParentOfferInstsByProdInstId(@KiteNotBlank String custId,
        @KiteNotBlank String prodInstId) {
        // TODO 待替确认获取全量数据的api
        List<KiteOfferInst> kiteOfferInstList = querySceneInstService.listAllParentOfferInstsByProdInstId(custId, prodInstId);
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transOfferInstDtoList(kiteOfferInstList));
    }

    /***
     * 场景使用--通过销售品实例ID查询实例对象
     *
     * @param sceneInstId
     * @param offerInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Override
    @Validated
    public DataResult<OfferInstDTO> getSceneOfferInstByOfferInstId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String offerInstId, @KiteNotBlank String custId) {
        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInst(custId, offerInstId);
        OfferInstDTO offerInstDTO = null;
        if (kiteOfferInst != null) {
            offerInstDTO = CopyToDtoUtil.packageOfferInstDTO(kiteOfferInst, null);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(offerInstDTO);
    }

    /**
     * 获取销售品实例的所有接入产品实例(成员明细)
     *
     * @param custId 客户id，分片字段，必填
     * @param offerInstId 销售品实例id(套餐销售品、可选包销售品、促销销售品)
     * @return 接入产品实例列表
     */
    @Override
    @Validated
    public DataResult<List<ProdInstDTO>> listAccessProdInstByOfferInstId(@KiteNotBlank String custId,
        @KiteNotBlank String offerInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInstsFromOfferInst(custId, offerInstId);
        List<ProdInstDTO> dtoList = CopyToDtoUtil.transProdInstDtoListForAccess(kiteProdInstList);
        // TODO 待确认 装箱
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询公共属性
     *
     * @param sceneInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Validated
    @Override
    public DataResult<List<InstAttrDTO>> listCommonAttrs(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String custId) {
        List<KiteAttr> kiteAttrList = querySceneInstService.listCommonAttrs(sceneInstId);
        Iterator<KiteAttr> items = kiteAttrList.iterator();
        while (items.hasNext()) {
            KiteAttr attr = items.next();
            if (StringUtils.equals(attr.getFieldName(), KeyConsts.HANDLER_CUST) && StringUtils.isNotEmpty(attr.getValueDesc())) {
                attr.setValueDesc(CustInfoEncryptUtils.setOrderHandleEncrypt(attr.getValueDesc()));
            }
            else {
                attr.setValueDesc(attr.getValueDesc());
            }

            if (!StringUtils.equals(KeyConsts.IFTRUE_T, attr.getShowType())) {
                items.remove();
            }
        }
        return ErrorConsts.SUCCESS.getResult(CopyToDtoUtil.transInstAttrDtoList(kiteAttrList));
    }

    /***
     * 根据fieldName查询公共属性
     *
     * @param sceneInstId
     * @param fieldName
     * @return
     */
    @Override
    public DataResult<KiteAttr> getCommonAttr(@KiteNotBlank String sceneInstId, @KiteNotBlank String fieldName) {
        return ErrorConsts.SUCCESS.getResult(querySceneInstService.getCommonAttr(sceneInstId, fieldName));
    }
}
